# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.hitbtc_v2.kr_rest.constants import (
    symbol_list,
    coin_list,
    native_quote_currency_list,
    quote_currency_list,
)
from coin.exchange.hitbtc_v2.kr_rest.currency import HitbtcCurrency


class HitbtcProduct(NativeProduct):
  _exchange = 'Hitbtc'
  NativeCurrencyType = HitbtcCurrency

  def __init__(self, base, quote):
    assert base.native_currency in coin_list, base
    assert quote.native_currency in native_quote_currency_list, quote
    assert base != quote, (base, quote)
    native_symbol = '%s%s' % (base.native_currency, quote.native_currency)
    if native_symbol not in symbol_list:
      assert quote.native_currency == 'USD', quote
      native_symbol = '%s%s' % (base.native_currency, 'USDT')
    assert native_symbol in symbol_list
    super().__init__(base, quote, native_symbol)

  @classmethod
  def FromStrNativeProductImpl(cls, product, current_datetime):
    assert isinstance(product, str), type(product)
    assert product in symbol_list, product
    quote_list = native_quote_currency_list + ['USDT']
    for quote in quote_list:
      if product.endswith(quote):
        base = product[:product.index(quote)]
        assert base in coin_list
        if quote == 'USDT':
          quote = 'USD'
        return HitbtcProduct(HitbtcCurrency.FromStrNativeCurrency(base),
                             HitbtcCurrency.FromStrNativeCurrency(quote))
    raise ValueError(product)
